/**
*	插件：websocket
*	开发者：雨中磐石
*	地址：www.rockoa.com
*	日期：2023-07-17
*/

js.websocket = {
	create:function(cans){
		var me = this;
		var defv = js.applyIf(cans,{
			id:'0',
			name:'',
			type:'p',
			url:'',recid:'',
			offline:false,
			isopenoff:true,
			linkci:0,
			onlinksuccess:function(){},
			onmessage:function(){return true;}
		});
		for(var i in defv)this[i]=defv[i];

		this.init = function(){
			if(!this.recid || !this.url)return;
			js.notify.init();
			var url = this.url+'';
			this.ws = new WebSocket(url);
			this.ws.onerror = function(e){
				me.relink();
			}
			this.ws.onopen = function(){
				me.openws();
			}
			this.ws.onclose= function(){
				me.relink();
			}
			this.ws.onmessage= function(d){
				me.onmessages(js.decode(d.data));
			}
		}
		this.openws = function(){
			this.linkci = 0;
			this.reclear();
			var s = '{"from":"'+this.recid+'","adminid":"'+this.type+''+this.id+'","sendname":"'+this.name+'","atype":"connect"}';
			this.linkbool = true;
			js.msg();
			this.send(s);
			this.onlinksuccess();
		}
		
		this.send = function(s){
			this.ws.send(s);
		}
		
		this.onmessages= function(d){
			this.onmessage(d);
			try{if(window['websocket_onmessage'])window['websocket_onmessage'](d);}catch(e){}
			var type = d.type;
			if(type=='todo'){
				if(d.mess && d.title){
					js.notify.send({
						body:jm.base64decode(d.mess),
						title:jm.base64decode(d.title),
						icon:d.icon,
						click:function(){
							xcy.openxiang({
								num:d.mknum,
								mid:d.mid,
								name:d.mkname,
								cnum:d.cnum,
								funitid:d.funitid,
								ismodal:false
							});
						}
					});
				}
			}
			if(type=='offoline'){
				this.offline = true;
				if(this.isopenoff)js.confirm('服务端在别处连接了，是否重新连接？', function(jg){
					if(jg=='yes'){
						me.relinks(0);
					}
				});
			}
		}
		
		this.relink = function(){
			this.linkbool = false;
			this.reclear();
			if(this.offline)return;
			js.msg('msg','服务端连接已断开<span id="timevsse">0</span>秒后重连', 60);
			this.linkci ++;
			this.relinks(3 * this.linkci);
			if(this.linkci == 10)this.linkci = 0;
		}
		
		this.reclear = function(){
			clearTimeout(this.relinktime);
		}
		
		this.relinks = function(sj){
			this.reclear();
			if(sj==0){
				this.offline = false;
				js.msg('wait','连接中...');
				this.init();
				return;
			}
			$('#timevsse').html(sj+'');
			this.relinktime = setTimeout(function(){me.relinks(sj-1)}, 1000);
		}
		
		this.init();
	}
}

/**
*	插件：通知信息
*	开发者：雨中磐石
*	地址：www.rockoa.com
*	日期：2023-07-17
*/

js.notify = {
	isopen:false,
	init:function(){
		var lx = this.getaccess();
		if(lx=='ok')this.isopen = true;//可以发送
		if(!this.isopen)console.log(''+NOWURL+'的通知的权限没打开');
		//js.notify.send({body:'诶诶过个二二二二二',title:'哈哈'});
	},

	getaccess:function(){
		if(nwjs)return 'ok';
		var lx = 'none';
		if(typeof(Notification)=='undefined'){
			lx='unlock';
			return lx;
		}
		lx = Notification.permission;
		if(lx=='granted'){lx='ok';}else if(lx=='denied'){lx='jz';}else{lx='mr';}
		return lx;
	},
	
	send:function(cans){
		if(!this.isopen)return;
		this.close();
		var can = js.applyIf(cans,{
			body:'',
			icon:'',
			tag:'rockplatnotice',
			title:'提醒',
			click:function(){}
		});
		var clsfun = function(){
			var blx = can.click();
			if(blx){
				if(js.nwjs)js.nwjs.show();
				window.focus();
			}
			js.notify.close();
		}
		if(nwjs){
			localStorage.setItem('xinhuplat_closelx','no');
			var url =NOWURL+'list/reim/notification.html?'+Math.random()+'';
			localStorage.setItem('xinhuplat_notification', JSON.stringify({icon:can.icon,title:can.title,body:can.body}));
			var canss={"frame": false,title:"todo","width": 350,resizable:false,'always_on_top':true,show:false,"height": 110,"show_in_taskbar":false,"transparent":true},me=this;
			nw.Window.open(url,canss,function(wis){
				me.notification = wis;
				wis.on('closed',function(){
					if(js.nwjs)js.nwjs.iconok();
					if(localStorage.getItem('xinhuplat_closelx')=='yes'){
						clsfun();
					}
					me.notification=false;
				});
			});
		}else{
			var notification= new Notification(can.title, can);
			notification.onclick = function(){
				clsfun();
			}
			this.notification = notification;
		}
	},
	close:function(){
		try{
			if(this.notification)this.notification.close(true);
		}catch(e){}
		this.notification = false;
	},
	paly:function(){
		var sid = 'notifys_sound_audio';
		var boa = get(sid);
		var src = 'res/sound/niu.mp3';
		if(!boa){
			var strsr = '<audio id="'+sid+'" src="'+src+'" autoplay="autoplay" hidden="true"></audio>';
			$('body').append(strsr);
		}
		boa = get(sid);
		boa.src=src;
		if(boa.play)boa.play();
	}
}